package com.southminority.ethnic.controller.admin.course;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.admin.vo.AdminPageReqVo;
import com.southminority.ethnic.controller.admin.vo.CourseMaterialCreateReqVo;
import com.southminority.ethnic.controller.admin.vo.CourseMaterialUpdateReqVo;
import com.southminority.ethnic.pojo.CourseMaterial;
import com.southminority.ethnic.service.course.CourseMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 课程资料后台管理Controller
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "课程资料管理后台", description = "管理员课程资料管理接口")
@RestController
@RequestMapping("/admin/course/material")
@CrossOrigin
@Validated
public class CourseMaterialAdminController {

    @Autowired
    private CourseMaterialService courseMaterialService;

    /**
     * 分页查询课程资料列表
     */
    @Operation(summary = "分页查询课程资料列表")
    @PostMapping("/page")
    public R<Page<CourseMaterial>> getMaterialPage(@Valid @RequestBody AdminPageReqVo reqVo) {
        try {
            Page<CourseMaterial> page = new Page<>(reqVo.getPageNum(), reqVo.getPageSize());
            
            // 构建查询条件
            Page<CourseMaterial> result = courseMaterialService.getMaterialPageWithKeyword(page, reqVo.getKeyword());
            
            log.info("管理员分页查询课程资料列表成功，页数: {}, 页面大小: {}, 关键词: {}", 
                    reqVo.getPageNum(), reqVo.getPageSize(), reqVo.getKeyword());
            return R.ok(result);
            
        } catch (Exception e) {
            log.error("管理员分页查询课程资料列表失败", e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取课程资料详情
     */
    @Operation(summary = "根据ID获取课程资料详情")
    @PostMapping("/detail/{id}")
    public R<CourseMaterial> getMaterialDetail(@PathVariable Long id) {
        try {
            CourseMaterial material = courseMaterialService.getById(id);
            if (material == null) {
                return R.fail("课程资料不存在");
            }
            
            log.info("管理员获取课程资料详情成功，资料ID: {}", id);
            return R.ok(material);
            
        } catch (Exception e) {
            log.error("管理员获取课程资料详情失败，资料ID: {}", id, e);
            return R.fail("获取详情失败：" + e.getMessage());
        }
    }

    /**
     * 创建课程资料
     */
    @Operation(summary = "创建课程资料")
    @PostMapping("/create")
    public synchronized R<CourseMaterial> createMaterial(@Valid @RequestBody CourseMaterialCreateReqVo reqVo) {
        try {
            CourseMaterial material = new CourseMaterial();
            BeanUtils.copyProperties(reqVo, material);
            
            boolean saved = courseMaterialService.save(material);
            if (saved) {
                log.info("管理员创建课程资料成功，资料名称: {}", material.getMaterialName());
                return R.ok(material);
            } else {
                return R.fail("创建失败");
            }
            
        } catch (Exception e) {
            log.error("管理员创建课程资料失败", e);
            return R.fail("创建失败：" + e.getMessage());
        }
    }

    /**
     * 更新课程资料
     */
    @Operation(summary = "更新课程资料")
    @PostMapping("/update")
    public synchronized R<CourseMaterial> updateMaterial(@Valid @RequestBody CourseMaterialUpdateReqVo reqVo) {
        try {
            CourseMaterial material = new CourseMaterial();
            BeanUtils.copyProperties(reqVo, material);
            
            boolean updated = courseMaterialService.updateById(material);
            if (updated) {
                log.info("管理员更新课程资料成功，资料ID: {}, 资料名称: {}", material.getId(), material.getMaterialName());
                return R.ok(material);
            } else {
                return R.fail("更新失败");
            }
            
        } catch (Exception e) {
            log.error("管理员更新课程资料失败，资料ID: {}", reqVo.getId(), e);
            return R.fail("更新失败：" + e.getMessage());
        }
    }

    /**
     * 删除课程资料
     */
    @Operation(summary = "删除课程资料")
    @PostMapping("/delete/{id}")
    public R<Boolean> deleteMaterial(@PathVariable Long id) {
        try {
            boolean deleted = courseMaterialService.removeById(id);
            if (deleted) {
                log.info("管理员删除课程资料成功，资料ID: {}", id);
                return R.ok(true);
            } else {
                return R.fail("删除失败");
            }
            
        } catch (Exception e) {
            log.error("管理员删除课程资料失败，资料ID: {}", id, e);
            return R.fail("删除失败：" + e.getMessage());
        }
    }
} 